package com.bibi.zoo.demo;

import com.alibaba.fastjson.JSONObject;
import com.bibi.zoo.pkg.resp.RespVerifyCode;
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
import org.apache.http.protocol.HttpContext;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.Arrays;
import java.util.List;

public class ProxyDemo {
    public static void main(String[] args) {
        List<Proxy> proxies = Arrays.asList(
                new Proxy(Proxy.Type.HTTP, new InetSocketAddress("60.217.158.163", 8060)),
                new Proxy(Proxy.Type.HTTP, new InetSocketAddress("159.138.5.222", 80)));
        for(int i=0; i<2;i++) {
            RestTemplate restTemplate = new RestTemplate();
            SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
            factory.setProxy(proxies.get(i));
            restTemplate.setRequestFactory(factory);
            String html = restTemplate.getForObject("http://200019.ip138.com/",String.class);
            Document doc = Jsoup.parse(html);
            String title = doc.getElementsByTag("title").get(0).text();
            System.out.println(title);
        }
    }
}
